package com.example.wan1.view.system;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.wan1.R;
import com.example.wan1.base.adapter.SubPagerAdapter;
import com.example.wan1.base.fragment.BaseFragment;
import com.example.wan1.bus.RxBus;
import com.example.wan1.utils.ToolsUtils;
import com.example.wan1.view.square.SquareFragment;
import com.example.wan1.view.system.knowledge.KnowledgeSystemFragment;
import com.example.wan1.view.system.navi.NaviFragment;
import com.example.wan1.view.system.question.QuestionFragment;
import com.example.wan1.view.system.website.WebsiteFragment;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

/***
 * @Author: zxd
 * @CreateDate: 2020/6/29 10:42
 * @Description: 知识体系
 * 1.知识体系
 * 2.导航
 * 3.常用网站
 * 4.问答
 */
public class SystemFragment extends BaseFragment {
    @BindView(R.id.system_tab)
    TabLayout mSystemTab;
    @BindView(R.id.system_vp)
    ViewPager mSystemVp;
    @Inject
    List<String> mTitles;
    @Inject
    List<Fragment> mFragments;
    @Inject
    SubPagerAdapter mAdapter;
    @BindView(R.id.fake_status_bar)
    View mFakeStatusBar;

    @Override
    protected void initData() {
        mTitles = new ArrayList<>();
        mFragments = new ArrayList<>();

        mTitles.add(getString(R.string.knowledge_title));
        mTitles.add(getString(R.string.knowledge_nav_title));
        mTitles.add(getString(R.string.knowledge_used_web_title));
        mTitles.add(getString(R.string.knowledge_question_title));
        mTitles.add(getString(R.string.square_tab_title));

        mFragments.add(new KnowledgeSystemFragment());
        mFragments.add(new NaviFragment());
        mFragments.add(new WebsiteFragment());
        mFragments.add(new QuestionFragment());
        mFragments.add(new SquareFragment());

        mAdapter.setData(mFragments, mTitles);
        mSystemVp.setAdapter(mAdapter);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    protected void initView() {
        mAdapter = new SubPagerAdapter(getChildFragmentManager(),
                FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        mSystemTab.setupWithViewPager(mSystemVp);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mSystemTab.setElevation(0);
        }
        int color = ToolsUtils.getThemeColor(getActivity());
        if (color != 0) {
            mSystemTab.setSelectedTabIndicatorColor(color);
            mSystemTab.setTabTextColors(Color.parseColor("#333333"), color);
        }
    }

    @Override
    protected int initLayoutRes() {
        return R.layout.fragment_system;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        RxBus.getDefault().subscribe(mContext, new RxBus.Callback<String>() {
            @Override
            public void onEvent(String s) {
                if (s.equals("theme_color")) {
                    int color = ToolsUtils.getThemeColor(getActivity());
                    mSystemTab.setSelectedTabIndicatorColor(color);
                    mSystemTab.setTabTextColors(Color.parseColor("#333333"), color);
                }
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        RxBus.getDefault().unregister(mContext);
    }

    public void setTvTitleBackgroundColor(int color) {
        mFakeStatusBar.setBackgroundColor(color);
    }
}
